package com.ch.control;

import com.ch.Result;
import com.ch.entity.Order;
import com.ch.entity.User;
import com.ch.exception.myexception;
import com.ch.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("order")
public class OrderControl {
    @Autowired
    private OrderService orderService;

    @GetMapping("setorder")
    public Result setorder(@RequestParam String name,
                           @RequestParam("price") int price,
                           @RequestParam String userName,
                           @RequestParam String pwd){
        Order order=new Order(name,price);
        User user=new User(userName,pwd,2);
        order.setUser(user);
        System.out.println(order);
        if (orderService.save(order)){
            return Result.success("数据已经插入到数据库");

        }
        else {
             new myexception(501,"数据插入错误");
             return Result.error();
        }
    }
    @GetMapping("getorder/{id}")
    public Result getOrder(@PathVariable int id,@RequestHeader("authorization") String token){
        Order order = orderService.getOrder(id,token);
        System.out.println("没有运行完orderservice 的 getorder(id) 的方法");
        if (order!=null){
            return Result.success(order);
        }
        else {
           new myexception(501,"没有得到相关的信息");
           return Result.error();
        }
    }
}
